package com.zoroki.student_score_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoroki.student_score_system.domain.User;
import com.zoroki.student_score_system.domain.UserClass;
import com.zoroki.student_score_system.service.UserClassService;
import com.zoroki.student_score_system.service.UserService;
import com.zoroki.student_score_system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-06-30 12:49:22
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public User userLogin(String username, String password) {
        QueryWrapper<User> eq = new QueryWrapper<User>().eq("username", username).eq("password", password);
        return userMapper.selectOne(eq,true);
    }

    @Override
    public User getUserInfo(String username) {
        QueryWrapper<User> eq = new QueryWrapper<User>().eq("username", username);
        return userMapper.selectOne(eq,true);
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateById(user)>0;
    }

    @Autowired
    private UserClassService userClassService;
    @Override
    public boolean deleteUserByUsername(String username) {

        //1. 根据用户名查找用户
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("username", username);
        User user = userMapper.selectOne(qw);

        System.out.println(user.getId()+"用户id");
        userClassService.deleteUserClass(user.getId());

        return userMapper.deleteById(user.getId())>0;
    }

    @Override
    public boolean addUser(User user) {
        return userMapper.insert(user)>0;
    }

    @Override
    public Page<User> getTeacherByPage(int pageNo, int pageSize, String username) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("role", "TEACHER" );
        // 1.查询用户
        if(!username.equals("")){
            qw.like(true,"username", username);
        }

        // 2. 构建分页条件：
        Page<User> page = new Page<>(pageNo, pageSize);

        // 3. 执行分页查询

        return userMapper.selectPage(page, qw);
    }

    @Override
    public Page<User> getStudentByPage(int pageNo, int pageSize, String username) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("role", "STUDENT" );
        // 1.查询用户
        if(!username.equals("")){
            qw.like(true,"username", username);
        }

        // 2. 构建分页条件：
        Page<User> page = new Page<>(pageNo, pageSize);

        // 3. 执行分页查询

        return userMapper.selectPage(page, qw);
    }

    @Override
    public Page<User> getAdminByPage(int pageNo, int pageSize, String username) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("role", "ADMIN" );
        // 1.查询用户
        if(!username.equals("")){
            qw.like(true,"username",username);
        }

        // 2. 构建分页条件：
        Page<User> page = new Page<>(pageNo, pageSize);

        // 3. 执行分页查询

        return userMapper.selectPage(page, qw);
    }
}




